<?php

namespace app\base\model\goods;

use think\Model;

class GoodsGroupInfo extends Model
{

    // 关闭自动写入时间戳
    protected $autoWriteTimestamp = false;

    protected $table = 'xy_goods.xy_goods_group_info';

    const GROUP_RUN_STATE = 1;

    const GROUP_SUCCESS_STATE = 2;

    const GROUP_ADMIN_CANCEL_STATE = 5;

    const GROUP_CANCEL_STATE = 4;

    public static function getInitiateListByUserId($user_id, $order = 'desc')
    {
        return self::where('user_id', $user_id)->order('create_time', $order)->select();
    }

    //批量获取指定用户发起的详细信息
    public static function getInitiateDetailListByUserIds($where = [], $order = [])
    {

        $query = self::alias('g')
            ->join('xy_goods.xy_goods_base_info b', 'g.goods_id = b.goods_id')
            ->join('xy_user.xy_user u', 'u.user_id = g.user_id')
            ->field('b.goods_id,b.goods_name,b.goods_title,b.goods_img_url,u.user_id,u.nick_name,u.gender,u.small_avatar_url as avatar_url,
            g.group_id,g.my_wish,g.need_people,g.join_people,g.need_wish_coin,g.group_total_price,g.group_state,
            g.valid_time,g.create_time,UNIX_TIMESTAMP(g.create_time) as create_timestamp,g.is_auto_group');

        if (empty($where)) {
            $where['g.group_state'] = ['<=', self::GROUP_SUCCESS_STATE];
        }

        if ($where) {
            $query->where($where);
        }

        if ($order) {
            $query->order($order);
        }


        $items = $query->select();


        return $items;
    }

    //批量获取指定用户发起的详细信息
    public static function getInitiateDetailListByGroupIds($group_ids, $order, $page, $page_size)
    {
        $items = self::alias('g')
            ->join('xy_goods.xy_goods_base_info b', 'g.goods_id = b.goods_id')
            ->join('xy_user.xy_user u', 'u.user_id = g.user_id')
            ->field('b.goods_id,b.goods_name,b.goods_title,b.goods_img_url,u.user_id,u.nick_name,u.gender,u.small_avatar_url as avatar_url,
            g.group_id,g.my_wish,g.need_people,g.join_people,g.need_wish_coin,g.group_total_price,g.group_state,
            g.valid_time,g.create_time,UNIX_TIMESTAMP(g.create_time) as create_timestamp, g.is_auto_group')
            ->whereIn('g.group_id', $group_ids)
            ->order($order)
            ->page($page, $page_size)
            ->select();

        return $items;
    }


    public static function getRecommendList($order = 'desc', $page = 1, $page_size = 10)
    {
        $items = self::alias('g')
            ->join('xy_goods.xy_goods_base_info b', 'g.goods_id = b.goods_id')
            ->join('xy_user.xy_user u', 'u.user_id = g.user_id')
            ->field('b.goods_id,b.goods_name,b.goods_title,b.goods_img_url,u.user_id,u.nick_name,u.gender,u.small_avatar_url as avatar_url,
            g.group_id,g.my_wish,g.need_people,g.join_people,g.need_gold,g.group_total_price,g.state,
            g.valid_time,g.create_time,UNIX_TIMESTAMP(g.create_time) as create_timestamp, g.is_auto_group')
            ->where('b.goods_label', 'recommend')
            ->order('g.create_time', $order)
            ->paginate(['page' => $page, 'list_rows' => $page_size])
            ->toArray();

        if (empty($items['data'])) {
            return [];
        }

        return $items['data'];
    }

    public static function getInitiateWishListByUserId($user_id, $state = 0, $page = 1, $page_size = 20)
    {
        $where['gp.user_id'] = $user_id;
        if ($state == 0) {
            $where['gp.group_state'] = ['in', [self::GROUP_RUN_STATE, self::GROUP_SUCCESS_STATE, self::GROUP_CANCEL_STATE]];
        } else {
            $where['gp.group_state'] = $state;
        }

        $items = self::alias('gp')->join('xy_goods.xy_goods_base_info gb', 'gb.goods_id=gp.goods_id', 'INNER')
            ->field('gb.goods_id,gb.goods_name,gb.goods_title,gb.goods_sub_title,gb.goods_img_url
         ,gp.group_id,gp.my_wish,gp.need_people,gp.join_people,gp.group_state,gp.create_time')
            ->where($where)
            ->order('gp.create_time', 'desc')
            ->page($page, $page_size)
            ->select();

        return $items;

    }


    public static function adminGetWishList($where, $query_string, $order, $page = 1, $page_size = 20)
    {
        $query = self::alias('gp')
            ->join('xy_goods.xy_goods_base_info gb', 'gb.goods_id=gp.goods_id', 'INNER')
            ->join('xy_goods.xy_goods_platform_info gpi', 'gb.goods_id=gpi.goods_id', 'INNER')
            ->join('(SELECT group_id as gp_id,max(UNIX_TIMESTAMP(update_time)) as last_support_time FROM `xy_goods`.`xy_goods_group_join_info` GROUP BY gp_id) `gj`', 'gp.group_id=gj.gp_id', 'LEFT')
            ->join('xy_user.xy_user user', 'user.user_id=gp.user_id')
            ->field('gb.goods_id,gb.goods_name,gb.goods_title,gb.goods_sub_title,gb.goods_img_url,
                            gpi.platform_name,gpi.platform_price,gpi.platform_url,
                            user.user_id,user.nick_name,user.gender,user.small_avatar_url,
                            gp.group_id,gp.my_wish,gp.need_people,gp.join_people,gp.group_state,gp.create_time,gp.success_time,gp.can_free_support,if(ISNULL(gj.last_support_time) , UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(gp.create_time),  UNIX_TIMESTAMP(now()) - gj.last_support_time) wait_support_time');

        if ($where) {
            $query->where($where);
        }

        if ($query_string) {
            $query->where($query_string);
        }

        if ($order) {
            $query->order($order);
        }

        $items = $query->paginate($page_size)->toArray();

        return $items;
    }

    public static function getInitiateList($where = [], $order = [])
    {
        $query = self::alias('g')
            ->join('xy_goods.xy_goods_base_info b', 'g.goods_id = b.goods_id')
            ->join('xy_user.xy_user u', 'u.user_id = g.user_id')
            ->field('b.goods_id,b.goods_name,b.goods_title,b.goods_img_url,u.user_id,u.nick_name,u.gender,u.small_avatar_url as avatar_url,g.group_id,g.my_wish,g.need_people,g.join_people,g.need_wish_coin,g.group_total_price,g.group_state,g.valid_time,g.create_time,UNIX_TIMESTAMP(g.create_time) as create_timestamp, g.is_auto_group');

        if ($order) {
            $query->order($order);
        }

        if (empty($where)) {
            $where['g.group_state'] = ['<=', self::GROUP_SUCCESS_STATE];
        }

        if ($where) {
            $query->where($where);
        }

        $items = $query->select();

        return $items;
    }

    public static function getWishNumByUserIds($where)
    {
        $query = new GoodsGroupInfo();

        if (empty($where)) {
            $where['group_state'] = ['<=', self::GROUP_SUCCESS_STATE];
        }

        if ($where) {
            $query->where($where);
        }

        return $query->count();
    }

    public static function getGoodsNameBYGroupId($group_id)
    {
        $goods_name = GoodsGroupInfo::alias('gi')->join('xy_goods.xy_goods_base_info gb', 'gi.goods_id=gb.goods_id')->where('gi.group_id', $group_id)->value('goods_name');

        return $goods_name;
    }


    public static function getFreeWishNum($user_id, $start_time = '', $end_time = '')
    {
        empty($start_time) && $start_time = date('Y-m-d H:i:s', strtotime('-0 days 00:00:00'));
        empty($end_time) && $end_time = date('Y-m-d H:i:s', strtotime('+1 days 00:00:00'));
        return self::where('user_id', $user_id)
            ->where('group_state', self::GROUP_RUN_STATE)
            ->where('can_free_support', 1)
            ->where('create_time', '>=', $start_time)
            ->where('create_time', '<', $end_time)
            ->count();
    }


}